package hu.blok1.veilingsite.client.gui;

import java.util.ArrayList;
import java.util.List;

import hu.blok1.veilingsite.client.Veilingsite;
import hu.blok1.veilingsite.client.data.ProductService;
import hu.blok1.veilingsite.client.data.ProductServiceAsync;
import hu.blok1.veilingsite.shared.Product;
import hu.blok1.veilingsite.shared.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CreateProductListGUI {
	private VerticalPanel vp;
	private VerticalPanel vpContent = new VerticalPanel();
	private ScrollPanel scroll = new ScrollPanel();
	private User user;
	private Veilingsite veilingsite;

	private List<Product> productList;
	private CellTable<Product> table;

	private FlexTable productFormTop = new FlexTable();
	
	private Button redraw = new Button();
	private Button searchProductButton = new Button();
	
	private int userID;

	private final ProductServiceAsync ProductService = GWT.create(ProductService.class);


	public CreateProductListGUI(VerticalPanel a, Veilingsite vs)
	{
		veilingsite = vs;
		user = veilingsite.getUser();
		userID = user.getId();

		vp = a;
		vp.clear();

		if(user.isLoggedIn())
		{		
			Label pageTitle = new Label("Productenoverzicht");
			Label separation = new Label(" --- ");
			
			searchProductButton.setText("Zoek");
			redraw.setText("Vernieuw");
			
			productFormTop.setCellSpacing(6);
			productFormTop.setWidget(0, 0, new Label("Specifiek product bekijken: "));
			productFormTop.setWidget(0, 1, searchProductButton);
			productFormTop.setWidget(1, 0, new Label("Productoverzicht vernieuwen: "));
			productFormTop.setWidget(1, 1, redraw);
			
			vpContent.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			vpContent.add(pageTitle);
			vpContent.add(separation);
			vpContent.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
			vpContent.add(productFormTop);
			vpContent.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			
			getProductData();
			
			redraw.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					getProductData();
				}
			});
			
			searchProductButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					// TODO Wellicht later verbeteren
					History.newItem("productSpecifications");
				}		
			});
		}
		else
		{
			Window.alert("U moet aangemeld zijn om producten in te kunnen zien!");
			//vpContent.add(new Label("Na inloggen kunt u het overzicht opvragen"));
			History.newItem("login");
		}
	}

	private void getProductData() {
		productList = null;
		table = null;
		productList = new ArrayList<Product>();
		table = new CellTable<Product>();

		createCellPanel();

		ProductService.findMultiProduct(new AsyncCallback<List<Product>>()
		{
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Product(en) opvragen is mislukt");
			}

			@Override
			public void onSuccess(List<Product> result) {
				if(result.isEmpty()) {
					Window.alert("Er zijn helaas geen producten gevonden");
				}
				else 
				{
					table.setRowData(result);
				}
			}
		});
		scroll.clear();
		scroll.setHeight("30em");	
		scroll.add(table);
		vpContent.setWidth("100%");
		vpContent.add(scroll);
		vp.add(vpContent);
	}
	
	private void createCellPanel() {
		table.setWidth("94%", true);
		table.setTableLayoutFixed(true);
		// Add a text column to show the id.				// 1
		TextColumn<Product> idColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getProductID());
			}
		};
//		idColumn.setSortable(true);
		table.addColumn(idColumn, "ID");
		table.setColumnWidth(idColumn, "5%");
		
		// Add a text column to show the name.				// 2
		TextColumn<Product> nameColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return object.getName();
			}
		};
//		nameColumn.setSortable(true);
		table.addColumn(nameColumn, "Naam");
		table.setColumnWidth(nameColumn, "15%");

		// Add a text column to show the minimumPrice.		// 3
		TextColumn<Product> minimumPriceColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getMinimumPrize());
			}
		};
		minimumPriceColumn.setSortable(true);
		table.addColumn(minimumPriceColumn, "Huidige prijs");
		table.setColumnWidth(minimumPriceColumn, "8%");
		
		// Add a text column to show the endDate.			// 5
		TextColumn<Product> endTimestampColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getEndDate());
			}
		};
//		startTimestampColumn.setSortable(true);
		table.addColumn(endTimestampColumn, "Einddatum");
		table.setColumnWidth(endTimestampColumn, "10%");

		// Add a text column to show the description.		// 7
		TextColumn<Product> descriptionColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getDescription());
			}
		};
		table.addColumn(descriptionColumn, "Beschrijving");
		table.setColumnWidth(descriptionColumn, "25%");

		// Add a text column to show the category.				// 10
		TextColumn<Product> categoryColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getCategory());
			}
		};
		table.addColumn(categoryColumn, "Categorie");
		table.setColumnWidth(categoryColumn, "13%");

		// Add a text column to show the userID.			// 9
		TextColumn<Product> userIDColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getUserID());
			}
		};
		
//		userIDColumn.setSortable(true);
		table.addColumn(userIDColumn, "ID aanbieder");
		table.setColumnWidth(userIDColumn, "8%");

		//table.getColumnSortList().push(endTimestampColumn);
		table.setRowCount(productList.size(), true);
		table.setRowData(0, productList);
	}
}